package com.oujiong.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oujiong.redisson.RedissonLock;
import com.oujiong.redisson.annotation.DistributedLock;

import lombok.extern.slf4j.Slf4j;

/**
 * 基于注解的方式 加锁
 */
@RestController
@Slf4j
public class AnnotatinLockController {

	@Autowired
	RedissonLock redissonLock;

	/**
	 * 模拟这个是商品库存
	 */
	public static volatile Integer TOTAL = 10;

	/**
	 * http://localhost:8086/annotatin-lock-decrease-stock
	 */
	@GetMapping("annotatin-lock-decrease-stock")
	@DistributedLock(value = "goods", leaseTime = 5)
	public String lockDecreaseStock() throws InterruptedException {
		if (AnnotatinLockController.TOTAL > 0) {
			AnnotatinLockController.TOTAL--;
		}
		AnnotatinLockController.log.info("===注解模式=== 减完库存后,当前库存===" + AnnotatinLockController.TOTAL);
		return "=================================";
	}
}
